import os

import numpy as np
from torch import nn
import torch


class DetectMultiBackend(nn.Module):
    # YOLOv5 MultiBackend class for python inference on various backends
    def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False, data=None, fp16=False, fuse=True):
        super().__init__()
        self.__dict__.update(locals())
        print("初始化...")

    def forward(self, im, augment=False, visualize=False):
        self.__dict__.update({})
        self.__dict__.update(locals())
        print("前向传播...")
        return self


def DetectMultiBackendTest():
    model = DetectMultiBackend()
    print(model.__dict__)
    pred = model(1)
    print(model.__dict__)
    print(pred.__dict__)


def test1():
    i = None
    if i:
        print("True")
    else:
        print("False")
    i = 3


def test2():
    LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1))  # https://pytorch.org/docs/stable/elastic/run.html
    RANK = int(os.getenv('RANK', -1))
    WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
    print(LOCAL_RANK)
    print(RANK)
    print(WORLD_SIZE)


def TensorStrctTest():
    t = torch.tensor([[178.40033, 26.79098, 395.32782, 89.88492, 0.89600, 0.00000],
                      [380.18814, 160.32484, 542.41052, 214.42458, 0.87617, 0.00000],
                      [202.06808, 313.09048, 337.06387, 356.58212, 0.86295, 0.00000]])
    print(t)
    print(t.shape)

    for i, det in enumerate(t):
        print(det, det.shape)

    print(t.view(-1))  # 转为一维张量


def TensorViewTest():
    a = torch.tensor([[1, 2, 3],
                      [4, 5, 6]])
    b = torch.tensor([[[1, 2],
                       [3, 4]],

                      [[5, 6],
                       [7, 8]],

                      [[9, 0],
                       [11, 12]]])
    print(a.view(-1))
    print(b.size())
    print(b.view(-1))
    print(b.view(3, -1))
    print(b.view(-1, 2 * 2))


def IterTest():
    list1 = [1, 2, 3, 4]
    list2 = [1, 2, 3, 4]
    for i in list1, list2:  # 先迭代list1，再迭代list2
        print(type(i))
        print(i)


def NumpyTest():
    import numpy as np
    print(str(np.interp(1, [0, 100], [1, 64 / 24])))
    print(str(np.interp(1, [0, 100], [1, 64 / 24]).round()))


def randintTest():
    for _ in range(10):
        i = np.random.randint(1)  # randint里面的数必须大于0
        print(i)


def strCatTest():
    print('123' + '456')

def floatTest():
    print("{:.2f}".format(1.2345))

if __name__ == '__main__':
    # DetectMultiBackendTest()
    # TensorStrctTest()
    # TensorViewTest()
    # test2()
    # IterTest()
    # NumpyTest()
    # randintTest()
    # strCatTest()
    floatTest()
    ...
